//
//  CustomerDetailViewController.m
//  Kfangchan
//
//  Created by liran on 14/11/14.
//  Copyright (c) 2014年 Beijing Renjuren Network Technology Co., Ltd. All rights reserved.
//

#import "CustomerDetailViewController.h"

@interface CustomerDetailViewController ()

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;

@property (strong, nonatomic) IBOutlet UIView *customerView;
@property (strong, nonatomic) IBOutlet UILabel *customerNameLabel;
@property (strong, nonatomic) IBOutlet UILabel *customerSexLabel;
@property (strong, nonatomic) IBOutlet UILabel *customerMobileLabel;
@property (strong, nonatomic) IBOutlet UILabel *idcardTitleLabel;
@property (strong, nonatomic) IBOutlet UILabel *idcardLabel;

@property (strong, nonatomic) IBOutlet UIView *customerRemarkView;
@property (strong, nonatomic) IBOutlet UILabel *customerRemarkLabel;


@property (strong, nonatomic) IBOutlet UIView *customerBuildingView;
@property (strong, nonatomic) IBOutlet UILabel *customerBuildingLabel;

@end

@implementation CustomerDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        
    self.scrollView.size = CGSizeMake(self.scrollView.width, [UIScreen mainScreen].bounds.size.height - self.navigationController.navigationBar.height - [UIApplication sharedApplication].statusBarFrame.size.height);
    
    self.navigationItem.titleView = [[Theam currentTheam] navigationTitleViewWithTitle:@"客户详情"];
    self.navigationItem.leftBarButtonItem = [[Theam currentTheam] navigationBarLeftButtonItemWithImage:[UIImage imageNamed:@"nav_back.png"] Target:self Selector:@selector(back)];
    
    [self initDetail];
    
    self.scrollView.contentSize = CGSizeMake(self.scrollView.width, self.customerBuildingView.origin.y + self.customerBuildingView.height);
    
    UIImageView *line = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"partition.png"]];
    line.frame = CGRectMake(0, self.customerView.height - 1, 320, 0.5);
    [self.scrollView addSubview:line];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark 客户详情构建
- (void)initDetail {
    if (self.entity) {
        // 姓名 性别 手机号
        [self initCustomer];
        // 备注
        [self initCustomerRemark];
        // 推荐楼盘
        [self initCustomerBuilding];
    }
}

- (void)initCustomer {
    self.customerNameLabel.text = self.entity.name;
    self.customerSexLabel.text = self.entity.sex;
    self.customerMobileLabel.text = self.entity.mobile;
    if (self.entity.idcard == nil || self.entity.idcard.length == 0) {
        self.idcardTitleLabel.hidden = YES;
        self.idcardLabel.hidden = YES;
        self.customerView.frame = CGRectAddHeight(self.customerView.frame, -26);
        self.customerRemarkView.origin = CGPointMake(self.customerRemarkView.origin.x, self.customerView.origin.y + self.customerView.size.height);
        self.customerBuildingView.origin = CGPointMake(self.customerBuildingView.origin.x, self.customerRemarkView.origin.y + self.customerRemarkView.size.height);
    } else {
        self.idcardLabel.text = self.entity.idcard;
    }
}

- (void)initCustomerRemark {
    self.customerRemarkLabel.text = self.entity.remark;
    float originalHeight = self.customerRemarkLabel.height;
    CGRect rect =  [self.customerRemarkLabel textRectForBounds:CGRectAddHeight(self.customerRemarkLabel.frame, 999) limitedToNumberOfLines:0];
    float differentHeight = rect.size.height - originalHeight;
    if (differentHeight > 0) {
        self.customerRemarkLabel.size = rect.size;
        self.customerRemarkView.frame = CGRectAddHeight(self.customerRemarkView.frame, differentHeight);
    }
}

- (void)initCustomerBuilding {
    CustomerBuildingMapList *buildingList = self.entity.buildingList;
    if (buildingList == nil || [buildingList isKindOfClass:[NSNull class]] || buildingList.count == 0) {
        self.customerBuildingView.size = CGSizeMake(self.customerBuildingView.width, 0);
        return;
    }
    self.customerBuildingView.origin = CGPointMake(self.customerBuildingView.origin.x, self.customerRemarkView.origin.y + self.customerRemarkView.height + 10);
    
    
    int count = (int)buildingList.count;
    self.customerBuildingLabel.text = [NSString stringWithFormat:@"推荐楼盘(%i)", count];
    float totalHeight = 0;
    for (int i=0; i<count; i++) {
        UIView *customerBuildingStatusView = [self getCustomerBuildingStatusView:buildingList[i]];
        if (customerBuildingStatusView == nil) {
            continue;
        }
        customerBuildingStatusView.origin = CGPointMake(0, i * (customerBuildingStatusView.height + 1) + self.customerBuildingView.height);
        //多加了一次
        totalHeight += customerBuildingStatusView.height + 1;
        [self.customerBuildingView addSubview:customerBuildingStatusView];
    }
    totalHeight -= 1;

    self.customerBuildingView.size = CGSizeMake(self.customerBuildingView.width, self.customerBuildingView.height + totalHeight);
}

-(UIView *)getCustomerBuildingStatusView:(CustomerBuildingMap *)customerBuildingMap {
    if (customerBuildingMap == nil) {
        return nil;
    }
    
    UIView *buildingStatusView = [[UIView alloc] init];
    buildingStatusView.backgroundColor = [UIColor whiteColor];
    buildingStatusView.size = CGSizeMake(self.customerBuildingView.width, 124);
    
    UILabel *buildingNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 290, 20)];
    buildingNameLabel.font = [UIFont systemFontOfSize:14];
    buildingNameLabel.text = customerBuildingMap.building.name;
    [buildingStatusView addSubview:buildingNameLabel];
    
    UILabel *buildingMapAddTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 80, 20)];
    buildingMapAddTimeLabel.font = [UIFont systemFontOfSize:14];
    buildingMapAddTimeLabel.textColor = [UIColor colorWithHexString:@"#8A8685"];
//    NSString *addTime = customerBuildingMap.addTime;
//    addTime = [addTime componentsSeparatedByCharactersInSet:[NSCharacterSet  whitespaceAndNewlineCharacterSet]][0];
    buildingMapAddTimeLabel.text = customerBuildingMap.addTimeStr;
    [buildingStatusView addSubview:buildingMapAddTimeLabel];
    
    UILabel *buildingMapStatusName1Label = [[UILabel alloc] initWithFrame:CGRectMake(100, 35, 80, 20)];
    buildingMapStatusName1Label.font = [UIFont systemFontOfSize:14];
    buildingMapStatusName1Label.textColor = [UIColor colorWithHexString:@"#8A8685"];
    buildingMapStatusName1Label.text = customerBuildingMap.statusName1;
    [buildingStatusView addSubview:buildingMapStatusName1Label];
    
    UIView *grayLineView = [[UIView alloc] initWithFrame:CGRectMake(15, 65, 290, 5)];
    grayLineView.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
    grayLineView.layer.cornerRadius = 2.5;
    [buildingStatusView addSubview:grayLineView];
    
    UIView *redLineView = [[UIView alloc] initWithFrame:grayLineView.frame];
    redLineView.backgroundColor = [UIColor colorWithHexString:@"#F04137"];
    redLineView.layer.cornerRadius = 2.5;
    redLineView.size = CGSizeMake(0, redLineView.height);
    [buildingStatusView addSubview:redLineView];
    
    NSArray *statusChangeList = customerBuildingMap.statusChangeList;
    float startX = 40;
    float totalWidth = 240;
    float interval = totalWidth/(statusChangeList.count - 1);
    float redLineViewWidth = 0;
    float frontLineWidth = 28.5;
    for (int i=0; i<statusChangeList.count; i++) {
        NSDictionary *statusChange = statusChangeList[i];
        NSString *statusName = statusChange[@"statusName"];
        NSString *statusUpTime = statusChange[@"statusUpTimeStr"];
        
        float x = startX + i * interval;
        float y = grayLineView.center.y;
        CGPoint point = CGPointMake(x, y);
        UIImageView *statusImageView = nil;
        if (statusUpTime == nil || [statusUpTime isKindOfClass:NSNull.class] || statusUpTime.length == 0) {
            statusImageView = [self getGrayImageView];
            statusUpTime = @"";
        } else {
            statusImageView = [self getRedImageView];
//            statusUpTime = [statusUpTime componentsSeparatedByCharactersInSet:[NSCharacterSet  whitespaceAndNewlineCharacterSet]][0];
//            NSArray *dateArray = [statusUpTime componentsSeparatedByString:@"-"];
//            NSString *year = dateArray[0];
//            year = [year substringFromIndex:2];
//            NSString *month = dateArray[1];
//            NSString *day = dateArray[2];
//            statusUpTime = [NSString stringWithFormat:@"%@/%@/%@", day, month, year];
            
            if (i == statusChangeList.count - 1) {
                redLineViewWidth = grayLineView.width;
            } else {
                redLineViewWidth = frontLineWidth + i * interval;
            }
        }
        statusImageView.center = point;
        [buildingStatusView addSubview:statusImageView];
        
        UILabel *statusNameLabel = [[UILabel alloc] init];
        statusNameLabel.size = CGSizeMake(30, 20);
        statusNameLabel.center = CGPointMake(statusImageView.center.x, statusImageView.center.y + 25);
        statusNameLabel.font = [UIFont systemFontOfSize:13];
        statusNameLabel.textColor = [UIColor colorWithHexString:@"#8A8685"];
        statusNameLabel.text = statusName;
        statusNameLabel.textAlignment = NSTextAlignmentCenter;
        [buildingStatusView addSubview:statusNameLabel];
        
        UILabel *statusUpTimeLabel = [[UILabel alloc] init];
        statusUpTimeLabel.size = CGSizeMake(70, 20);
        statusUpTimeLabel.center = CGPointMake(statusNameLabel.center.x, statusNameLabel.center.y + 20);
        statusUpTimeLabel.font = [UIFont systemFontOfSize:13];
        statusUpTimeLabel.textColor = [UIColor colorWithHexString:@"#8A8685"];
        statusUpTimeLabel.text = statusUpTime;
        statusUpTimeLabel.textAlignment = NSTextAlignmentCenter;
        [buildingStatusView addSubview:statusUpTimeLabel];
    }
    redLineView.size = CGSizeMake(redLineViewWidth, redLineView.height);
    
    return buildingStatusView;
}

- (UIImageView *)getRedImageView {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"customer_detail_red.png"]];
    imageView.size = CGSizeMake(23, 23);
    return imageView;
}

- (UIImageView *)getGrayImageView {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"customer_detail_gray.png"]];
    imageView.size = CGSizeMake(23, 23);
    return imageView;
}

#pragma mark -
#pragma mark
- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)call:(id)sender
{
    UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"拨打电话:%@", self.entity.mobile] delegate:self cancelButtonTitle:@"取消" otherButtonTitles: @"确定", nil];
    [alterView show];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex != alertView.cancelButtonIndex) {
        NSString *phone = self.entity.mobile;
        NSString *tel = [NSString stringWithFormat:@"tel://%@", phone];
        
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:tel]]) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel]];
        }
    }
}

@end
